'use strict'

$(function(){
    let id = location.search.split('?')[1]
    console.log(id);
    if(id){
        $.get(`http://localhost:3000/product`,data=>{
            let newdata = data.filter(item=>{
                return item.id == id
            })
            $('[name=id]').val(newdata[0].id)
            $('[name=name]').val(newdata[0].name)
            $('[name=price]').val(newdata[0].price)
        })
    }
})

function save(){
    let obj = {
        id:$('[name=id]').val(),
        name:$('[name=name]').val(),
        price:$('[name=price]').val(),
    }
    if(obj.id){
        putProduct(obj.id,obj).then(data=>{
            if(data.code==1000){
                location.href='./index.html'
            }else{
                alert(code.msg)
            }
        })
    }else{
        postProduct(obj).then(data=>{
            if(data.code===1000){
                location.href='./index.html'
            }else{
                alert(data.msg)
            }
        })
    }
}

function cancel(){
    location.href='./index.html'
}